package org.jeecg.modules.school.consult.controller;/*
 @author ycz
 @date 2022-01-18-14:10
*/

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.school.consult.entity.MemSchoolCaseShunt;
import org.jeecg.modules.school.consult.service.MemSchoolCaseShuntService;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/school/consult/shunt")
@Slf4j
public class MemSchoolCaseShuntController {

    @Value("${jeecg.path.upload}")
    private String upLoadPath;

    @Autowired
    private MemSchoolCaseShuntService caseShuntService;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Object list(MemSchoolCaseShunt caseShunt,
                       @RequestParam(value = "pageNo",defaultValue = "1")Integer pageNo,
                       @RequestParam(value = "pageSize" , defaultValue = "10")Integer pageSize,
                       HttpServletRequest request
                       ){
        Result<IPage<MemSchoolCaseShunt>> result=new Result<>();

        QueryWrapper<MemSchoolCaseShunt> queryWrapper= QueryGenerator.initQueryWrapper(caseShunt,request.getParameterMap());

        Page<MemSchoolCaseShunt> page=new Page<>(pageNo,pageSize);
        Page<MemSchoolCaseShunt> pageList=caseShuntService.page(page,queryWrapper);

        result.setSuccess(true);
        result.setResult(pageList);

        log.info("個案分流的pageList====>"+pageList.getRecords());
        return result;
    }
    /**
     * 導出個案分流
     */
    @RequestMapping(value = "/exportXls")
    public Object exportXls(MemSchoolCaseShunt memSchoolCaseShunt,HttpServletRequest request){

        QueryWrapper<MemSchoolCaseShunt> queryWrapper=QueryGenerator.initQueryWrapper(memSchoolCaseShunt,request.getParameterMap());

        ModelAndView mv=new ModelAndView(new JeecgEntityExcelView());

        String selections = request.getParameter("selections");
        if (!oConvertUtils.isEmpty(selections)){ //不為空的話
            queryWrapper.in("id",selections.split(","));
        }
        List<MemSchoolCaseShunt> pageList = this.caseShuntService.list(queryWrapper);
        mv.addObject(NormalExcelConstants.FILE_NAME,"個案分流");
        mv.addObject(NormalExcelConstants.CLASS,MemSchoolCaseShunt.class);
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();//TODO
        log.info("導出人：=》"+user);
        String tempRealname;
        if (user==null){
            tempRealname="未知";
        }else {
            tempRealname=user.getRealname();
        }
        ExportParams exportParams=new ExportParams("個案分流列表","導出人："+tempRealname,"導出信息");
        exportParams.setImageBasePath(upLoadPath);
        mv.addObject(NormalExcelConstants.PARAMS, exportParams);
        mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
        log.info("===========導出結束========================");
        return mv;
    }





}
